/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.entity.factory.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.entity.factory.api.IHistoryProcessInstanceVOFactory;
import com.wilmar.process.entity.vo.HistoryProcessInstanceVO;
import com.wilmar.process.enumerate.EnumHistoryProcessInstanceVOProperties;
import com.wilmar.process.enumerate.EnumProcessInstanceState;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 May 12, 2010
 * @since 1.0.0
 * 
 */
public class HistoryProcessInstanceVOFactoryImpl extends
		BaseValueObjectFactoryImpl<HistoryProcessInstanceVO> implements
		IHistoryProcessInstanceVOFactory {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -7845174653418688787L;

	/**
	 * @see com.wilmar.process.entity.factory.api.IBaseValueObjectFactory#getValueObject(java.sql.ResultSet)
	 */
	public List<HistoryProcessInstanceVO> getValueObject(ResultSet resultSet)
			throws SQLException {
		List<HistoryProcessInstanceVO> result = new ArrayList<HistoryProcessInstanceVO>();
		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);
		while (resultSet.next()) {
			HistoryProcessInstanceVO vo = new HistoryProcessInstanceVO();
			vo
					.setD_startTime(resultSet
							.getTimestamp(EnumHistoryProcessInstanceVOProperties.START_TIME
									.getColumnName()));
			if (vo.getD_startTime() != null)
				vo.setStartTime(sdf.format(vo.getD_startTime()));
			else
				vo.setStartTime("");
			vo
					.setD_endTime(resultSet
							.getTimestamp(EnumHistoryProcessInstanceVOProperties.END_TIME
									.getColumnName()));
			if (vo.getD_endTime() != null)
				vo.setEndTime(sdf.format(vo.getD_endTime()));
			else
				vo.setEndTime("");
			vo.setLogId(super.getLong(resultSet,
					EnumHistoryProcessInstanceVOProperties.START_LOG_ID));
			vo
					.setKey(super
							.getString(
									resultSet,
									EnumHistoryProcessInstanceVOProperties.PROCESS_INSTANCE_KEY));
			vo
					.setProcessDefinitionId(super
							.getString(
									resultSet,
									EnumHistoryProcessInstanceVOProperties.PROCESS_DEFINITION_ID));
			vo
					.setProcessDefinitionName(super
							.getString(
									resultSet,
									EnumHistoryProcessInstanceVOProperties.PROCESS_DEFINITION_NAME));
			vo
					.setProcessInstanceId(super
							.getString(
									resultSet,
									EnumHistoryProcessInstanceVOProperties.PROCESS_INSTANCE_ID));
			vo.setProcessInstanceKey(vo.getKey());
			vo.setInstanceStarter(super.getString(resultSet,
					EnumHistoryProcessInstanceVOProperties.INSTANCE_STARTER));
			vo.setDuration(super.getLong(resultSet,
					EnumHistoryProcessInstanceVOProperties.DURATION));
			vo.setState(super.getString(resultSet,
					EnumHistoryProcessInstanceVOProperties.STATE));
			String activities = super.getString(resultSet,
					EnumHistoryProcessInstanceVOProperties.ACTIVITY_NAME);
			if (activities != null && activities.length() > 0) {
				String[] a_activities = activities
						.split(ProcessSystemDeclareKeys.COMMA);
				Set<String> s_activities = new HashSet<String>();
				for (String activity : a_activities) {
					s_activities.add(activity);
				}
				vo.setActivityNames(s_activities);
			}
			EnumProcessInstanceState e_state = EnumProcessInstanceState
					.valueOf(vo.getState().toUpperCase());
			if (e_state == EnumProcessInstanceState.ACTIVE) {
				vo.setStopAble(true);
				vo.setSuspendAble(true);
				vo.setResumeAble(false);
			} else if (e_state == EnumProcessInstanceState.SUSPENDED) {
				vo.setStopAble(true);
				vo.setSuspendAble(false);
				vo.setResumeAble(true);
			}
			result.add(vo);
		}
		return result;
	}

}
